package tools.hmmer;

import java.util.Vector;

import tools.parents.Alignment;
import tools.sort.mQuickSort;

public class hmmerAlignment extends Alignment{

	public hmmerMultipleModelAlignment[] hmm_top;
	public hmmerModelAlignment[] hmms;
	//name of the searched transcript
	public String qname;
	//Accession of the searched transcript
	public String qaccession;
	//Description of the searched transcript
	public String qdescription;
	
	public hmmerAlignment(int top_size,int size){
		hmm_top=new hmmerMultipleModelAlignment[top_size];
		hmms=new hmmerModelAlignment[size];
	}
	public hmmerModelAlignment[] getTopDomainsByEvalue()throws Exception{
		hmmerModelAlignment[] top;
		Vector<hmmerModelAlignment> out=new Vector<hmmerModelAlignment>();
		if(hmms.length>0){
			Vector<hmmerModelAlignment> sorted=new Vector<hmmerModelAlignment>();
			for(int i=0;i<hmms.length;i++){
				hmms[i].setCompare('e'); //dangerous stuff! will live on 
				sorted.add(hmms[i]);
			}
			mQuickSort.sort(sorted);
			out.add(sorted.get(0));
			for(int i=1;i<sorted.size();i++){
				hmmerModelAlignment cur=(hmmerModelAlignment)sorted.get(i);
				int j;
				for(j=0;j<out.size();j++){
					hmmerModelAlignment tmp=(hmmerModelAlignment)out.get(j);
					if(tmp.tstart<cur.tstart){
						if(tmp.tend>=cur.tstart){
							j=-1;
							break;
						}
					}else if(tmp.tstart<=cur.tend){
						j=-1;
						break;
					}else
						break;
				}
				if(j>=0)
					out.add(j,cur);
			}
		}
		top=new hmmerModelAlignment[out.size()];
		for(int i=0;i<out.size();i++)
			top[i]=(hmmerModelAlignment)out.get(i);
		return top;
	}
	
	public hmmerModelAlignment[] getTopDomainsByScore()throws Exception{
		hmmerModelAlignment[] top;
		Vector<hmmerModelAlignment> out=new Vector<hmmerModelAlignment>();
		if(hmms.length>0){
			Vector<hmmerModelAlignment> sorted=new Vector<hmmerModelAlignment>();
			for(int i=0;i<hmms.length;i++)
				sorted.add(hmms[i]);
			mQuickSort.sort(sorted);
			out.add(sorted.get(0));
			for(int i=1;i<sorted.size();i++){
				hmmerModelAlignment cur=(hmmerModelAlignment)sorted.get(i);
				int j;
				for(j=0;j<out.size();j++){
					hmmerModelAlignment tmp=(hmmerModelAlignment)out.get(j);
					if(tmp.tstart<cur.tstart){
						if(tmp.tend>=cur.tstart){
							j=-1;
							break;
						}
					}else if(tmp.tstart<=cur.tend){
						j=-1;
						break;
					}else
						break;
				}
				if(j>=0)
					out.add(j,cur);
			}
		}
		top=new hmmerModelAlignment[out.size()];
		for(int i=0;i<out.size();i++)
			top[i]=(hmmerModelAlignment)out.get(i);
		return top;
	}
}
